//
//  BuildServerViewController.m
//  InviteJob
//
//  Created by chirui on 2017/10/13.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "BuildServerViewController.h"
#import "MainTableViewCell.h"
#import "ImageTextButton.h"
#import "MainSearchViewController.h"
#import "PublishImageViewController.h"
#import "BuilsServerDetailsViewController.h"
#import "PublishHistoryViewController.h"
#import "WorkDetailsViewController.h"
#import "ItemButton.h"
#import "BusinessCategorySV.h"
@interface BuildServerViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>{
    
    UITableView * _tableView;
    UIView * searchView;
    UIImageView * bgImage;
    
    NSMutableArray * _dataArray;
    NSArray * titleArray;
    
    int _page;
    int _totailPage;
    
    NSString * codeStr;
    NSString * classStr;
    
    NSMutableArray *_dataImage;
    NSMutableArray *_dataImageAll;
    
    UIView * heardView;
    BusinessCategorySV *businessCategorySV;
}

@property (nonatomic, strong) ImageTextButton * areButton;

@end

@implementation BuildServerViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    [self creatTableView];
    [self creatSearchView];
    
    _page = 1;
    _dataArray = @[].mutableCopy;
    _dataImage = @[].mutableCopy;
    _dataImageAll = @[].mutableCopy;
    
    codeStr = @"0";
    classStr = @"";
    
    titleArray  = @[@"施工队",@"投标融资",@"设计外包",@"造价资料",
                    @"办营资安",@"证才中介",@"公司转让",@"教育培训",
                    @"建材供应",@"机械供应",@"二手租赁",@"房屋出租",
                    @"税法法务",@"维管后勤",@"特殊其他"];
    NSArray * image = @[@"icon_menu_service_team",@"icon_menu_service_bid",@"icon_menu_service_design",@"icon_menu_service_data",@"icon_menu_service_business_license",@"icon_menu_service_intermediary",@"icon_menu_service_company_transfer",@"icon_menu_service_education",@"icon_menu_service_supply",@"icon_menu_service_mechanics",@"icon_menu_service_lease",@"icon_menu_service_rental",@"icon_menu_service_monely",@"icon_menu_service_servuu",@"icon_menu_service_more"];
    
    
    CGRect bcFrame = CGRectMake(0, 160, WIDTH, 185);
    businessCategorySV = [BusinessCategorySV businessCategorySVWithFrame:bcFrame imageArray:image titleArray:titleArray target:self action:@selector(ClassBtnClick:)];
    // picker
    self.pickerView = [[RegionPicker alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.pickerView.pickerView.dataSource = self;
    self.pickerView.pickerView.delegate = self;
    [self.pickerView.confirmButton addTarget:self action:@selector(regionSelectAction) forControlEvents:UIControlEventTouchUpInside];
    [self.pickerView setHidden:YES];
    
     UIWindow *window = [[UIApplication sharedApplication].delegate window];
    [window addSubview:self.pickerView];
    
    [self getLoadImage];
    [self getMessageListData];
    
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(server:) name:@"server" object:nil];
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
   
}
-(void)server:(NSNotification *)sender{
    [self hideNoDataView];
    if ([sender.userInfo[@"server"] intValue] == 1) {
        
        _page = 1;
        [self getMessageListData];

    }
}

-(void)creatSearchView{
    
    self.areButton = [[ImageTextButton alloc] initWithFrame:CGRectMake(0, 0, 60,40 )
                                                      image:[UIImage imageNamed:@"icon_nav_drop_down"]
                                                      title:@"  全 国"];
    
    [self.areButton addTarget:self action:@selector(chooseAreClick:) forControlEvents:UIControlEventTouchUpInside];
    self.areButton.buttonTitleWithImageAlignment = UIButtonTitleWithImageAlignmentRight;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.areButton];
    
    self.navigationItem.titleView= [SearchView creatSearchTextFieldForDelegate:self Placeholder:@"搜索关键词"];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightItemWithIcon:@"icon_nav_more" target:self action:@selector(rightBtnClick:)];
    
    bgImage =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 95, 0, 80, 85)];
    bgImage.image = ImageName(@"img_more_bg");
    bgImage.userInteractionEnabled = YES;
    bgImage.hidden = YES;
    [self.view addSubview:bgImage];
    
    [self creatBtnFrame:CGRectMake(0, 7, 80, 35) andTitle:@"发布" andImageName:@"icon_nav_more_release" andSelector:@selector(publishBtnClick:) onView:bgImage];
    
    [self creatBtnFrame:CGRectMake(0, 50, 80, 35) andTitle:@"历史" andImageName:@"icon_nav_more_history" andSelector:@selector(historyBtnClick:) onView:bgImage];
    
    UIView * line =[[UIView alloc]initWithFrame:CGRectMake(0, 45, 80, 0.5)];
    line.backgroundColor =[UIColor whiteColor];
    [bgImage addSubview:line];
}

-(void)chooseAreClick:(UIButton *)btn {

    if(!self.province) {
        
        NSString *jsonPath = [[NSBundle mainBundle]pathForResource:@"region" ofType:@"json"];
        NSData *jsonData = [[NSData alloc]initWithContentsOfFile:jsonPath];
        self.province = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
        self.city = self.province[0][@"city"];
        self.country = self.city[0][@"country"];
        self.mem_province = self.province[0][@"province_code"];
        self.mem_city = self.city[0][@"city_code"];
        self.mem_country = self.country[0][@"country_code"];
        [self.pickerView setHidden:NO];
        [self.pickerView.pickerView reloadAllComponents];
    }else {
        [self.pickerView setHidden:NO];
        [self.pickerView.pickerView reloadAllComponents];
    }


}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    MainSearchViewController * search =[[MainSearchViewController alloc]init];
    search.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:search animated:YES];
}

-(void)rightBtnClick:(UIButton *)btn{
    isUser
    if (!user.user_id) {
        SHuiMBhudTextTimeStr(@"请先登录");
        return;
    }
    bgImage.hidden = !bgImage.hidden;
}

-(void)publishBtnClick:(UIButton *)btn{
    
    bgImage.hidden =YES;
    
    PublishImageViewController * publish =[[PublishImageViewController alloc]init];
    publish.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:publish animated:YES];
}

-(void)historyBtnClick:(UIButton *)btn{
    
    bgImage.hidden =YES;
    
    PublishHistoryViewController * publish =[[PublishHistoryViewController alloc]init];
    publish.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:publish animated:YES];
}

-(void)creatTableView{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-48) style:UITableViewStyleGrouped];
    _tableView.dataSource =self;
    _tableView.delegate =self;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_tableView registerNib:[UINib nibWithNibName:[MainTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[MainTableViewCell reuseIdentifier]];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        [self getLoadImage];
        [self getMessageListData];
        
    }];
    _tableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _page ++;
        [self getMessageListData];
        
    }];
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    
    if (_dataArray.count>10) {
        [self hideNoDataView];
        _tableView.mj_footer.hidden = NO;
    }else{
        _tableView.mj_footer.hidden = YES;
    }

    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
     NSDictionary * dic = _dataArray[indexPath.row];
    UILabel * contLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 70, WIDTH -40, 20)];
    contLabel.text  = dic[@"title"];
    contLabel.font =[UIFont systemFontOfSize:15];
    contLabel.numberOfLines = 0;
    [contLabel sizeToFit];
    
//    UIImageView * imageView;
//    NSArray * imageArray;
//    
//    if ([dic[@"img_srcs"] isKindOfClass:[NSArray class]]) {
//        
//        imageArray = dic[@"img_srcs"];
//        
//        if (imageArray.count>3) {
//            for (int i=0; i<3; i++) {
//                
//                imageView =[[UIImageView alloc]initWithFrame:CGRectMake(20+10*i+(WIDTH-60)/3*i, CGRectGetMaxY(contLabel.frame)+10, (WIDTH-60)/3, (WIDTH-60)/3)];
//                [imageView sd_setImageWithURL:ImageUrl(imageArray[i]) placeholderImage:ImageName(@"img_dynamic_1")];
//            }
//        }else{
//        
//        for (int i=0; i<imageArray.count; i++) {
//            
//            imageView =[[UIImageView alloc]initWithFrame:CGRectMake(20+10*i+(WIDTH-60)/3*i, CGRectGetMaxY(contLabel.frame)+10, (WIDTH-60)/3, (WIDTH-60)/3)];
//            [imageView sd_setImageWithURL:ImageUrl(imageArray[i]) placeholderImage:ImageName(@"img_dynamic_1")];
//         }
//        }
//    }
//    
//    if (imageArray.count ==0) {
    
    return CGRectGetMaxY(contLabel.frame)+20;
//        
//    }else {
//
//    return CGRectGetMaxY(imageView.frame)+20;
//    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 355;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld",indexPath.section);
    static NSString * iden = @"cellid";
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:iden];
    }else{
        while ([cell.contentView.subviews lastObject] != nil) {
            
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    NSDictionary * dic = _dataArray[indexPath.row];
    UIView * bgView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 70)];
    bgView.backgroundColor =[UIColor whiteColor];
    
    UIImageView * userIcon =[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
    
    [userIcon sd_setImageWithURL:ImageUrl(dic[@"avatar"]) placeholderImage:ImageName(@"img_default_head")];
    userIcon.layer.cornerRadius = 25.f;
    userIcon.layer.masksToBounds =YES;
    [bgView addSubview:userIcon];
    
    
    UILabel * nameLabel =[[UILabel alloc]initWithFrame:CGRectMake(70, 13, WIDTH - 90, 20)];
    nameLabel.font =[UIFont boldSystemFontOfSize:15];
    nameLabel.textColor = RGB(0x333333);
    nameLabel.text = dic[@"user_name"];
    [bgView addSubview:nameLabel];
    
    UILabel * timeLabel =[[UILabel alloc]initWithFrame:CGRectMake(70, 40, WIDTH -90, 20)];
    timeLabel.font =[UIFont systemFontOfSize:13];
    timeLabel.textColor = [UIColor grayColor];
    timeLabel.text = dic[@"update_time"];
    [bgView addSubview:timeLabel];
    
    UILabel * kindLabel =[[UILabel alloc]initWithFrame:CGRectMake(WIDTH-80, 13, 65, 30)];
    kindLabel.font =[UIFont systemFontOfSize:13];
    kindLabel.textColor = strColor(@"ff7800");
    kindLabel.text = dic[@"service_type"];
    kindLabel.textAlignment = NSTextAlignmentCenter;
    kindLabel.layer.cornerRadius = 3;
    kindLabel.layer.borderWidth = 0.5;
    kindLabel.layer.borderColor = strColor(@"ff7800").CGColor;
    [bgView addSubview:kindLabel];
    
    
    UILabel * contLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(userIcon.frame)+10, WIDTH -40, 20)];
    
    if ([USER_DEFAULTGet(@"server") isEqualToString:@"project"]) {
        contLabel.text  = [NSString stringWithFormat:@"业务需求：%@",dic[@"title"]];
    }else{
        contLabel.text  = [NSString stringWithFormat:@"业务范围：%@",dic[@"title"]];
    }
    contLabel.font =[UIFont systemFontOfSize:15];
    contLabel.numberOfLines = 0;
    [contLabel sizeToFit];
    [bgView addSubview:contLabel];
    
//    UIImageView * imageView;
//    NSArray * imageArray;
//    
//    if ([dic[@"img_srcs"] isKindOfClass:[NSArray class]]) {
//        
//        imageArray = dic[@"img_srcs"];
//        if (imageArray.count>3) {
//        
//        for (int i=0; i<3; i++) {
//            
//            imageView =[[UIImageView alloc]initWithFrame:CGRectMake(20+10*i+(WIDTH-60)/3*i, CGRectGetMaxY(contLabel.frame)+10, (WIDTH-60)/3, (WIDTH-60)/3)];
//            [imageView sd_setImageWithURL:ImageUrl(imageArray[i]) placeholderImage:ImageName(@"img_dynamic_1")];
//            imageView.contentMode = UIViewContentModeScaleAspectFill;
//            imageView.clipsToBounds = YES;
//            [bgView addSubview:imageView];
//        }
//        }else {
//            for (int i=0; i<imageArray.count; i++) {
//                
//                imageView =[[UIImageView alloc]initWithFrame:CGRectMake(20+10*i+(WIDTH-60)/3*i, CGRectGetMaxY(contLabel.frame)+10, (WIDTH-60)/3, (WIDTH-60)/3)];
//                [imageView sd_setImageWithURL:ImageUrl(imageArray[i]) placeholderImage:ImageName(@"img_dynamic_1")];
//                imageView.contentMode = UIViewContentModeScaleAspectFill;
//                imageView.clipsToBounds = YES;
//                [bgView addSubview:imageView];
//         }
//        }
//    }
//   
//    if (imageArray.count==0) {
         bgView.frame = CGRectMake(0, 0, WIDTH, CGRectGetMaxY(contLabel.frame)+20);
        
//    }else {
//    
//      bgView.frame = CGRectMake(0, 0, WIDTH, CGRectGetMaxY(imageView.frame)+20);
//    }
    
    
    
    UIView * lineView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(bgView.frame)-10, WIDTH, 10)];
    lineView.backgroundColor =strColor(@"f6f6f6");
    [bgView addSubview:lineView];
    
    [cell.contentView addSubview:bgView];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = _dataArray[indexPath.row];
    BuilsServerDetailsViewController * details =[[BuilsServerDetailsViewController alloc]init];
    details.hidesBottomBarWhenPushed = YES;
    details.severID = dic[@"service_id"];
    [self.navigationController pushViewController:details animated:YES];
    
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    heardView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 270)];
    heardView.backgroundColor =strColor(@"f6f6f6");
    
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,0, WIDTH, 160) delegate:self placeholderImage:[UIImage imageNamed:@"moren_banner_image"]];
    cycleScrollView.backgroundColor =Color(229, 229, 229);
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycleScrollView.currentPageDotColor = NavBackColor;
    [heardView addSubview:cycleScrollView];
    cycleScrollView.localizationImageNamesGroup = _dataImage;
    cycleScrollView.clickItemOperationBlock = ^(NSInteger index) {
        //岗位详情
        if ([_dataImageAll[index][@"type"] intValue] == 1) {
            WorkDetailsViewController * work =[[WorkDetailsViewController alloc]init];
            work.hidesBottomBarWhenPushed = YES;
            work.workIdStr=_dataImageAll[index][@"link"];
            [self.navigationController pushViewController:work animated:YES];
            return ;
        }
        
        if ([_dataImageAll[index][@"type"] intValue] == 2) {
            BuilsServerDetailsViewController * details =[[BuilsServerDetailsViewController alloc]init];
            details.hidesBottomBarWhenPushed = YES;
            details.severID=_dataImageAll[index][@"link"];
            [self.navigationController pushViewController:details animated:YES];
            return;
        }
    };
    
    [heardView addSubview:businessCategorySV];
 
    return heardView;
}

-(void)ClassBtnClick:(UIButton *)btn{
    
    classStr = titleArray[btn.tag -100];
    
    _page = 1;
    [self hideNoDataView];
    [self getMessageListData];
    
}



-(void)getMessageListData{
    
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
     
    NSString *server = USER_DEFAULTGet(@"server");
    
    
    NSDictionary * dic = @{@"page":[NSString stringWithFormat:@"%d",_page],
                           @"perpage":@"10",
                           @"code":codeStr,
                           @"service_type":classStr,
                           @"type":server};
    
    SHuiMBhubNoTimeLoding;
    [PostNetWorking postNetWorkingStr:RequestUrlStr(BuildService_list_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            _totailPage = [responseObject[@"datas"][@"total_page"]  intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
                    [self hideNoDataView];
                    if (_dataArray.count<10) {
                        
                        _tableView.mj_footer.hidden = YES;
                    }else {
                        _tableView.mj_footer.hidden = NO;
                    }
                }else {
                    _tableView.mj_footer.hidden = YES;
                    [self shopNoDataViewBackgroundColor:[UIColor clearColor] withImage:@"img_home_no_news" withLabelMsg:@"没有任何数据" withButton:@"" isY:1];
                }
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];
                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
    
}
#pragma mark -- 轮播图
-(void)getLoadImage{
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(getBanner_api) andparameters:@{@"sign_key":@"app-company"} Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {
                
                _dataImageAll = responseObject[@"datas"];
                
                for (NSDictionary *dic in responseObject[@"datas"]) {
                    [_dataImage addObject:dic[@"image"]];
                }
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [_tableView reloadData];
                });
            }
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
}
// 区域选择确定
- (void)regionSelectAction {
    [self.pickerView setHidden:YES];
    
    if (self.selectedRegion.length==0) {

        codeStr = @"110101";
        [self.areButton setTitle:@"东城区" forState:UIControlStateNormal];
    }else{
         NSArray * array = [self.selectedRegion componentsSeparatedByString:@" "];
        
        NSString * are = [array lastObject];
        
        if (are.length>3) {
            
            are = [are  substringToIndex:3];
        }
        
         [self.areButton setTitle:are forState:UIControlStateNormal];
    }
    
    _page = 1;
    
    [self getMessageListData];
}

#pragma mark - UIPickerView datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (component) {
        case 0:
        {
            if(![self.province isKindOfClass:[NSNull class]]) {
                return self.province.count;
            }else {
                return 0;
            }
        }
            break;
        case 1:
        {
            if(![self.city isKindOfClass:[NSNull class]]) {
                return self.city.count;
            }else {
                return 0;
            }
        }
            break;
        case 2:
        {
            if(![self.country isKindOfClass:[NSNull class]]) {
                return self.country.count;
            }else {
                return 0;
            }
        }
            break;
        default:
            break;
    }
    return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    switch (component) {
            
        case 0:
        {
            // 省份
            return self.province[row][@"simple_province_name"];
        }
            break;
        case 1:
        {
            // 城市
            return self.city[row][@"simple_city_name"];
        }
            break;
        case 2:
        {
            // 乡镇、街道
            return self.country[row][@"simple_country_name"];
        }
            break;
        default:
            break;
    }
    return nil;
}

#pragma mark - UIPickerView delegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0:
        {
            // 省份
            self.city = self.province[row][@"city"];
            self.country = self.city[0][@"country"];
            [pickerView selectRow:0 inComponent:1 animated:NO];
            [self pickerView:self.pickerView.pickerView didSelectRow:0 inComponent:1];
        }
            break;
        case 1:
        {
            // 城市
            self.country = self.city[row][@"country"];
            [pickerView selectRow:0 inComponent:2 animated:NO];
            [self pickerView:self.pickerView.pickerView didSelectRow:0 inComponent:2];
        }
            break;
        case 2:
        {
            // 乡镇、街道
        }
            break;
        default:
            break;
    }
    
    self.mem_province = self.province[[pickerView selectedRowInComponent:0]][@"province_code"];
    self.selectedRegion = self.province[[pickerView selectedRowInComponent:0]][@"province_name"];
    
    
    self.mem_city = self.city[[pickerView selectedRowInComponent:1]][@"city_code"];
    self.selectedRegion = [self.selectedRegion stringByAppendingString:[NSString stringWithFormat:@" %@",self.city[[pickerView selectedRowInComponent:1]][@"city_name"]]];
    
    
    if([self.country isKindOfClass:[NSArray class]]) {
        self.mem_country = self.country[[pickerView selectedRowInComponent:2]][@"country_code"];
        
        self.selectedRegion = [self.selectedRegion stringByAppendingString:[NSString stringWithFormat:@" %@",self.country[[pickerView selectedRowInComponent:2]][@"country_name"]]];
        
    }else {
        self.mem_country = @"1";
    }
    
    codeStr = self.mem_country;
    
    [pickerView reloadAllComponents];
}



-(UIView *)creatBtnViewFrame:(CGRect)rec andBorderColor:(UIColor *)col onView:(UIView *)view {
    
    UIView * btnView =[[UIView alloc]init];
    btnView.frame = rec;
    btnView.layer.borderWidth = 0.7f;
    btnView.layer.borderColor = col.CGColor;
    btnView.backgroundColor =[UIColor whiteColor];
    //    btnView.layer.cornerRadius = 5.f;
    //    btnView.layer.masksToBounds =YES;
    [view addSubview:btnView];
    
    return btnView;
}


-(void)creatBtnFrame:(CGRect)rec andTitle:(NSString *)title andImageName:(NSString *)imageName andSelector:(SEL)sel onView:(UIView *)view{
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = rec;
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setImage:ImageName(imageName) forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
}

 
@end
